import { Container } from "../Container";
import { BaseGui } from "../Services/BaseGui";
import { DataService } from "../Services/DataService";
import { CameraAnimation } from "../Services/view-anim/CameraAnimation";
import { ViewerService } from "../Services/ViewerService";
import { ViewPointManService } from "../Services/ViewPointManService";

export class ViewManGui extends BaseGui {
    title = `视点管理[${ViewManGui.name}]`;
    ViewerService = Container.defineProperty(ViewerService);
    DataService = Container.defineProperty(DataService);
    ViewPointManService = Container.defineProperty(ViewPointManService);
    getData() {
        return this.ViewPointManService.getData();
    }
    async UpdateGui() {
        let viewer = await this.ViewerService.ViewerPromise;
        let gui = this.gui;
        var fun = {
            add: () => {
                let array = this.getData();
                let name = "viewpoint" + (array.length + 1);
                const newData = { name: name, view: viewer.getState() };
                this.ViewPointManService.getViewerState(newData);
                array.push(newData);
                fun.data = array.length - 1;
                fun._dataChange();
                updateGUI();
            },
            remove: () => {
                let array = this.getData();
                array.splice(fun.data, 1);
                fun.data = fun.data < array.length ? fun.data : fun.data - 1;
                fun._dataChange();
                updateGUI();
            },
            data: -1,
            dataName: "",
            // playAnimation: () => {
            // },
            //autoSelect:false,
            _viewselect:()=>{
                // let array = this.getData();
                // viewer.restoreState(array[fun.data].view, { viewport: true });
                //this.ViewPointManService.viewSelect(fun.data);
                let array = this.getData();
                this.ViewPointManService.restoreViewerState(array[fun.data]);
            },
            _dataOptions: () => {
                let array = this.getData();
                let options = {} as any;
                array.forEach((v: { name: any; }, i: number) => {
                    const name = `[${i + 1}]${v.name}`;
                    options[name] = i;
                });
                return options;
            },
            _dataChange: () => {
                let array = this.getData();
                let data = array[fun.data];
                fun.dataName = data.name;
                updateGUI();
                fun._viewselect();
            },
            _dataNameChanged:()=>{
                let array = this.getData();
                array[fun.data].name = fun.dataName;
                updateGUI();
            }
        }

        let updateGUI = () => {
            Array.from(gui.__controllers).forEach(_ => {
                gui.remove(_);
            });
            this.guiService.buildGUIByObject(gui,fun);
        }
        updateGUI();
        return this;
    }
    Init(): void {
        this.DataService.OnDataChanged.addEventListener(()=>{
            this.Refresh();
        });
    }
}